package cn.huaqingcheng.tianshu.mybatis;

import cn.huaqingcheng.tianshu.common.core.model.AttributeMap;
import cn.huaqingcheng.tianshu.common.core.model.JsonArray;
import cn.huaqingcheng.tianshu.mybatis.type.AttributeMapTypeHandler;
import cn.huaqingcheng.tianshu.mybatis.type.JsonArrayTypeHandler;
import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import org.apache.ibatis.type.TypeHandlerRegistry;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * MybatisPlusConfig
 */
@Configuration(proxyBeanMethods = false)
public class MybatisPlusConfig {

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        return interceptor;
    }

    @Bean
    public ConfigurationCustomizer configurationCustomizer() {
        return configuration -> {
            TypeHandlerRegistry registry = configuration.getTypeHandlerRegistry();
            registry.register(AttributeMap.class, AttributeMapTypeHandler.class);
            registry.register(JsonArray.class, JsonArrayTypeHandler.class);
        };
    }

}
